/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef UCOP_DATA_REL_FIELD_H_
#define UCOP_DATA_REL_FIELD_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <cstdint>
#include <string>
#include "Ucop/Macros.h"


namespace Ucop {
namespace Data {
namespace Rel {

    class Field
    {
    public:
        enum DataTypes
        {
            TYPE_UNKNOWN = 0x00,
            TYPE_STRING = 0x01,
            TYPE_INTEGER = 0x02,
            TYPE_FLOAT = 0x03,
            TYPE_BOOL = 0x04
        };

    public:
        Field(void);
        Field(const char* _Myvalue, enum DataTypes _Mytype);
        ~Field(void);

    public:
        void SetType(enum DataTypes _Mytype) { _Mytype = _Mytype; }
        enum DataTypes type(void) const { return _Mytype; }
		void SetValue(const char* _Value) { _Myvalue = _Value; }
        bool IsNull(void) const { return _Myvalue == NULL; }
        const char* c_str(void) const { return _Myvalue; }

        std::string AsString(void) const { return std::string(((_Myvalue != NULL) ? _Myvalue : "")); }
        float AsFloat(void) const { return _Myvalue ? static_cast<float>(atof(_Myvalue)) : 0.0f; }
        bool AsBool(void) const { return _Myvalue ? atoi(_Myvalue) > 0 : false; }
        double AsDouble(void) const { return _Myvalue ? static_cast<double>(atof(_Myvalue)) : 0.0f; }
        std::int8_t AsInt8(void) const { return _Myvalue ? static_cast<std::int8_t>(atol(_Myvalue)) : std::int8_t(0); }
        std::int32_t AsInt32(void) const { return _Myvalue ? static_cast<std::int32_t>(atol(_Myvalue)) : std::int32_t(0); }
        std::uint8_t AsUInt8(void) const { return _Myvalue ? static_cast<std::uint8_t>(atol(_Myvalue)) : std::uint8_t(0); }
        std::uint16_t AsUInt16(void) const { return _Myvalue ? static_cast<std::uint16_t>(atol(_Myvalue)) : std::uint16_t(0); }
        std::int16_t AsInt16(void) const { return _Myvalue ? static_cast<std::int16_t>(atol(_Myvalue)) : std::int16_t(0); }
        std::uint32_t AsUInt32(void) const { return _Myvalue ? static_cast<std::uint32_t>(atol(_Myvalue)) : std::uint32_t(0); }
        std::uint64_t AsUInt64(void) const;
        std::int64_t AsInt64(void) const;

    private:
        const char* _Myvalue;
        enum DataTypes  _Mytype;
        DISALLOW_COPY_AND_ASSIGN(Field);
    };

}}}

#endif // UCOP_DATA_REL_FIELD_H_
